package com.fz.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.security.PublicKey;

/**
 * Created by webrx on 2017/6/26.
 */
@Controller @RequestMapping("/user")
public class User {
    @RequestMapping("/t1.action")
    public ModelAndView t1(HttpServletRequest req){
        System.out.println(req.getParameter("id"));
        ModelAndView mv = new ModelAndView("t1");
        mv.addObject("id",req.getParameter("id"));
        //request.setAttribute("id",id);
        return mv;
    }
    @RequestMapping("/t2") @ResponseBody
    public void t2(HttpServletRequest req , HttpServletResponse resp) throws IOException {
        resp.setContentType("text/html;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        PrintWriter out = resp.getWriter();
        out.print("<h1>id="+req.getParameter("id")+"</h1>");

    }
    @RequestMapping("/t3")
    public String t3(){

        //return "t3";//转发方式1 WEB-INF/template/t3.jsp视图来显示数据

       // return "forward:t3.jsp";//转发方式1//localhost/user/t3.jsp
        return "forward:/t3.jsp";//转发到localhost/t3.jsp
        //return "redirect:/t3.jsp";//重定向到localhost/t3.jsp
       // return "redirect:t3.jsp";//localhost/user/t3.jsp
       // return "redirect:t3.action";//重定向localhost/user/t3.jsp
    }


    @RequestMapping("/t4.action") @ResponseBody
    public String t4(){
        return "hello-world-你好";
    }
    //必须是post请求//http://localhost/user/t5.jsp
    @RequestMapping(value = "t5",method = RequestMethod.GET) @ResponseBody
    public void t5(){
        System.out.println("hello");

    }
    @RequestMapping(value = "t6" ,method = RequestMethod.GET) @ResponseBody
    public void t6(String name){
        System.out.println(name);

    }
    //Springmvc RestFul 风格形式
    // http://localhost/user/t7/100.action
    //100=> {id} int id
    @RequestMapping(value = "/t7/{id}/{name}" ,method = RequestMethod.GET) @ResponseBody
    public void t7(@PathVariable int id ,@PathVariable String name){
        System.out.println(id+"="+name);

    }
    @RequestMapping("/t8.action")
    public ModelAndView t8(HttpServletRequest req , HttpServletResponse resp ){
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        String ss =req.getParameter("id");
        ModelAndView mv = new ModelAndView("wo/shoping");
        mv.addObject("id",ss);
        System.out.println(ss);
        return mv;
    }

}
